package com.zjitc.controller;

import static java.awt.SystemColor.info;

import com.github.pagehelper.PageInfo;
import com.zjitc.common.ServerResponse;
import com.zjitc.model.Backstage;
import com.zjitc.model.Pay;
import com.zjitc.model.Product;
import com.zjitc.model.User;
import com.zjitc.service.BackstageService;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/31 14:58
 * @description:
 */
@Controller
@RequestMapping("/backstage")
public class BackstageController {

  @Autowired
  private BackstageService backstageService;

  @RequestMapping("/backstageLogin")
  public String backstageLogin(
      @RequestParam(name = "backstagename", required = false) String backstagename,
      @RequestParam(name = "backstagepassword", required = false) String backstagepassword,
      HttpSession session, HttpServletResponse res
  ) throws IOException {
    if (backstagename == null && backstagepassword == null) {
      return "admin/backstage";
    }
    ServerResponse<User> response = backstageService.login(backstagename, backstagepassword);
    if (response.success()) {
      session.setAttribute("user", response.getData());
      return "redirect:/backstage/backstageshowProduct.do";
    }
    session.setAttribute("msg", response.getMsg());
    return "admin/backstage";

  }

  @RequestMapping("/backstageshowProduct")
  public String backstageshowProduct(HttpSession session) {
    List<Product> backstage = backstageService.backstageshowProduct();
    session.setAttribute("backstageshowProduct", backstage);
    return "admin/background";
  }

  @RequestMapping("/backstagedelete")
  public String backstagedelete(Integer id,HttpSession session){
    backstageService.backstagedelete(id);
    return "redirect:/backstage/backstageshowProduct.do";
  }

  @RequestMapping("/updatebackstage")
  public String updatebackstage(Integer id,HttpSession session){
    Product product= backstageService.findproductById(id);
    session.setAttribute("product",product);
    return "admin/update";
  }

  @RequestMapping("/updateback")
  public String updateback(Product product){
    backstageService.updateback(product);
    return "redirect:/backstage/backstageshowProduct.do";
  }

  @RequestMapping("/userList")
  public String userList(HttpSession session) {
    List<User> userList = backstageService.findAllUser();
    session.setAttribute("users", userList);
    return "admin/admin";
  }

  @RequestMapping("/userEditbackstage")
  public String userEditbackstage(Integer id,HttpSession session){
    User user=backstageService.findUserById(id);
    session.setAttribute("user",user);
    return"admin/Useredit";
  }

  @RequestMapping("/userEdit")
  public String userEdit(User user){
    backstageService.userEdit(user);
    return "redirect:/backstage/userList.do";
  }

  @RequestMapping("/userdelete")
  public String userdelete(Integer id){
    backstageService.userdelete(id);
    return "redirect:/backstage/userList.do";
  }

  @RequestMapping("/findMyPay")
  public String findMyPay(HttpSession session){
    List<Pay> pay=backstageService.findMyPay();
    session.setAttribute("pay",pay);
    return "admin/record";
  }

  @RequestMapping("/paydelete")
  public String paydelete(Integer id){
    backstageService.paydelete(id);
    return "redirect:/backstage/findMyPay.do";
  }
}

